"""
@Author : 合肥一元数智教育科技有限公司
@Date :  2025/3/14 10:44
@Description : 
   通过缓存的方式实现单例模式    装饰器
"""
# 装饰器
def singleton(cls):

    cache_instance = {}

    def inner(name):
        print('--------------inner------------------')
        # 判定创建的对象是都存在  cache_instance
        if cls not in cache_instance:
            print('----------not in---------------')
            cache_instance[cls] = cls(name)   # 缓存数据   Person()
        return cache_instance[cls]

    return inner


@singleton    # Person = singleton(Person)
class Person:
    def __init__(self, name):
        self.name = name

    def __str__(self):
        return self.name


p1 = Person('tom')
print(p1.name,id(p1))

p2 = Person('jack')
print(p2.name,id(p2))

p3 = Person('xiaoming')
print(p3.name,id(p3))

